package bancosys.tec.security.impl.services.incluir;

import jmine.tec.database.pack.PackException;
import jmine.tec.database.pack.services.PackEntityPersistenceHelper;
import jmine.tec.di.annotation.Injected;
import jmine.tec.services.annotations.Execution;
import jmine.tec.services.annotations.Input;
import jmine.tec.services.annotations.Output;
import jmine.tec.services.annotations.ServiceImplementor;
import jmine.tec.services.api.ActionsEnum;
import jmine.tec.services.api.export.ReferenceMap;
import jmine.tec.services.api.export.ServiceFiller;
import jmine.tec.services.export.impl.ExportUtils;
import jmine.tec.services.io.ServiceBean;
import bancosys.tec.security.impl.domain.MetaCredential;

/**
 * Seviço para incluir Meta Credenciais.
 * 
 * @author seiti
 * @created Oct 11, 2010
 */
@ServiceImplementor(action = ActionsEnum.INCLUIR)
public class MetaCredentialService implements ServiceFiller<MetaCredential> {

    public static final String DATABASE_ID = "Database ID";

    public static final String IDENTIFICADOR = "Identificador";

    public static final String NAME = "Nome";

    public static final String DESCRIPTION = "Descrição";

    // Injected
    private PackEntityPersistenceHelper helper;

    // Parameters
    private Long databaseId;

    private String metaCredentialName;

    private String metaCredentialDescription;

    /**
     * @return uma meta credential
     * @throws PackException PackException
     */
    @Execution
    @Output(propertyName = IDENTIFICADOR)
    public MetaCredential execute() throws PackException {
        MetaCredential mc = this.helper.findOrCreate(MetaCredential.class, this.databaseId, this.metaCredentialName);
        mc.setName(this.metaCredentialName);
        mc.setDescription(this.metaCredentialDescription);
        this.helper.save(mc, this.databaseId);
        return mc;
    }

    /**
     * {@inheritDoc}
     */
    public void fillServiceBean(ServiceBean bean, ReferenceMap referenceMap, MetaCredential entity) {
        ExportUtils.setService(bean, this.getClass());
        ExportUtils.put(bean, IDENTIFICADOR, referenceMap.addReference(entity, "metaCredential"));
        ExportUtils.put(bean, NAME, entity.getName());
        ExportUtils.put(bean, DESCRIPTION, entity.getDescription());
        ExportUtils.putDatabaseId(bean, DATABASE_ID, entity);
    }

    /**
     * @param helper the helper to set
     */
    @Injected
    public void setHelper(PackEntityPersistenceHelper helper) {
        this.helper = helper;
    }

    /**
     * @param databaseId the databaseId to set
     */
    @Input(fieldName = DATABASE_ID, required = false)
    public void setDatabaseId(Long databaseId) {
        this.databaseId = databaseId;
    }

    /**
     * @param name to set
     */
    @Input(fieldName = NAME)
    public void setName(String name) {
        this.metaCredentialName = name;
    }

    /**
     * @param description to set
     */
    @Input(fieldName = DESCRIPTION, required = false)
    public void setDescription(String description) {
        this.metaCredentialDescription = description;
    }
}
